// rgblink_device.h
#ifndef RGBLINK_DEVICE_H_
#define RGBLINK_DEVICE_H_
#include "rgblinkProtocol.h"
#include "crossdevice.h"

class RgblinkDevice {
public:
	/**************************************************************
	 * Default constructor: create an instance of our serial interface
	 *   object.
	 * Parameters: None, though it could be convenient to have default
	 *   settings specified here as well as possibly a default port name.
	 * Returns: Nothing(it is a constructor)
	 */
	RgblinkDevice(RGBlinkProtocolCallback callback, SendBufferFunc sendBuffer, int bufferSize);
	/*
	 * Default deconstructor: delete an instance of out serial interface
	 */
	~RgblinkDevice();
	
	static CrossDeviceList* getDeviceList(void);
	
	bool open(char* deviceName);
	bool close();
	void setSpeed(int speed);
	/*
	*   parity: parity to be used. One of
	 *     'n' = None
	 *     'e' = Even
	 *     'o' = Odd
	*/
	void setParity(char parity);
	
	void receive_buffer(BYTE *buffer, int len);
	void task(void);
	int send(int add, int cmd, int dat1, int dat2, int dat3, int dat4);
	int sendNoWait(int add, int cmd, int dat1, int dat2, int dat3, int dat4);
	int sendCmd(int add, Command_TypeDef *cmd);
	int sendCmdAndWait(int add, Command_TypeDef *cmd);
	int sendCmdIsAck(int add);
	int sendCmdAndDataBlock(int add, Command_TypeDef *cmd, char *buffer, int size); // size 包括checksum
	int sendAndWait(int add, int cmd, int dat1, int dat2, int dat3, int dat4);
	int waitAck(int add, int timeout);
	int readPipeBuffer( char *buffer, int size, int timeout);
	int is_connect(int add);
	/*
	 * readBuf: read in a buffer of words
	 * Parameters:
	 *   buf: The buffer to read words into
	 *   bufSize: the number of words to read. This should be less than
	 *     or equal to the size of buf
	 * Returns: the number of words actually read
	 */
	int readBuf(char * buf, int bufSize);
	int readBufAndWritePipe(char *buf, int bufSize);
	/*
	 * writeBuf: write out a buffer of words
	 * Parameters:
	 *   buf: The buffer to write words from
	 *   bufSize: the number of words to write. This should be less than
	 *     or equal to the size of buf
	 * Returns: the number of words actually written
	 */
	int writeBuf(char * buf, int bufSize);
	
	char *getRemoteIP(void);

private:
	void sendBuf(char * buf, int bufSize);
	void operator()(char * buf, int bufSize);
	
	RGBlinkProtocol *device_Protocol;
	Pipe_t *device_pipe;
	BYTE *device_buffer;
	CrossDevice *_crossDevice;
};


























#endif
